package com.kyleswebspace.propertyanalyzer.gui;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.FocusEvent;
import java.awt.event.ItemEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;

import com.kyleswebspace.parser.FieldParser;
import com.kyleswebspace.propertyanalyzer.config.ArcaFont;
import com.kyleswebspace.propertyanalyzer.config.BorderBox;
import com.kyleswebspace.propertyanalyzer.config.ColorBox;
import com.kyleswebspace.propertyanalyzer.model.DataModel;

public class CashRequirementsPanel extends JPanel implements Updateable,
		KeyListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7962657477945393570L;

	private DataModel dm;
	private FocusMediator fm;

	private JLabel downPaymentLabel;
	private JTextField downPaymentField;
	private JLabel purchaseCostsLabel;
	private JTextField purchaseCostsField;
	private JLabel lessCashBackLabel;
	private JTextField lessCashBackField;
	private JLabel totalCashRequiredLabel;
	private JTextField totalCashRequiredField;

	private Color innerPanelColor = ColorBox.innerPanelColor;
	private Color editableColor = ColorBox.fieldColor;
	private Font appFont = ArcaFont.appFont;

	public CashRequirementsPanel(DataModel dm, FocusMediator fm) {
		this.dm = dm;
		this.fm = fm;
		dm.register(this);

		this.setLayout(new GridBagLayout());
		TitledBorder title;
		title = BorderFactory.createTitledBorder("Cash Requirements");
		this.setBorder(title);
		this.setBackground(innerPanelColor);

		GridBagConstraints constraints = new GridBagConstraints();
		constraints.ipadx = 20; // PADDING

		// down payment
		downPaymentLabel = new JLabel("Down Payment");
		downPaymentLabel.setFont(appFont);
		constraints.gridx = 0;
		constraints.gridy = 0;
		constraints.gridwidth = 3;
		constraints.anchor = GridBagConstraints.WEST;
		this.add(downPaymentLabel, constraints);
		downPaymentField = new JTextField(4);
		downPaymentField.addFocusListener(this);
		downPaymentField.setFocusable(false);
		downPaymentField.setFont(appFont);
		downPaymentField.setBorder(BorderBox.fieldBorder);
		downPaymentField
				.setToolTipText("<html>This value is automatically<br/>"
						+ "generated for you and is simply<br/>"
						+ "the difference between the<br/>"
						+ "purchase price and the amount of<br/>"
						+ "financing.</html>");
		downPaymentField.setHorizontalAlignment(JTextField.RIGHT);
		constraints.gridwidth = 1;
		constraints.gridx = 3;
		constraints.anchor = GridBagConstraints.EAST;
		this.add(downPaymentField, constraints);

		// purchase costs
		purchaseCostsLabel = new JLabel("Purchase Costs                   +");
		purchaseCostsLabel.setFont(appFont);
		constraints.gridx = 0;
		constraints.gridy = 1;
		constraints.gridwidth = 3;
		constraints.anchor = GridBagConstraints.WEST;
		this.add(purchaseCostsLabel, constraints);
		purchaseCostsField = new JTextField(4);
		purchaseCostsField.addFocusListener(this);
		purchaseCostsField.setFocusable(false);
		purchaseCostsField.setFont(appFont);
		purchaseCostsField.setBorder(BorderBox.fieldBorder);
		purchaseCostsField.setToolTipText("<html>The purchase costs are<br/>"
				+ "calculated above and may<br/>"
				+ "include a seller rebate for<br/>"
				+ "items such as land transfer<br/>"
				+ "tax, building inspections etc.</html>");
		purchaseCostsField.setHorizontalAlignment(JTextField.RIGHT);
		constraints.gridwidth = 1;
		constraints.gridx = 3;
		constraints.anchor = GridBagConstraints.EAST;
		this.add(purchaseCostsField, constraints);

		// less: cash back
		lessCashBackLabel = new JLabel("LESS: Cash Back                 -");
		lessCashBackLabel.setFont(appFont);
		constraints.gridx = 0;
		constraints.gridy = 2;
		constraints.gridwidth = 3;
		constraints.anchor = GridBagConstraints.WEST;
		this.add(lessCashBackLabel, constraints);
		lessCashBackField = new JTextField(4);
		fm.setLessCashBackField(lessCashBackField);
		lessCashBackField.addFocusListener(this);
		lessCashBackField.addKeyListener(this);
		lessCashBackField.setFont(appFont);
		lessCashBackField.setBorder(BorderBox.fieldBorder);
		lessCashBackField.setBackground(editableColor);
		lessCashBackField
				.setToolTipText("<html>This space is for your calculations<br/>"
						+ "only because a bank will not like<br/>"
						+ "to see a value in this space,<br/>"
						+ "assuming the bank will see this<br/>"
						+ "analysis. If the vendor agrees to<br/>"
						+ "give you $XX.xx at time of closing<br/>"
						+ "then that value would be inserted<br/>"
						+ "here to see what the effect is to<br/>"
						+ "your bottom line.</html>");
		lessCashBackField.setHorizontalAlignment(JTextField.RIGHT);
		constraints.gridwidth = 1;
		constraints.gridx = 3;
		constraints.anchor = GridBagConstraints.EAST;
		this.add(lessCashBackField, constraints);

		// separator
		constraints.gridx = 0;
		constraints.gridwidth = 4;
		constraints.gridy = 3;
		constraints.fill = GridBagConstraints.HORIZONTAL;
		this.add(new JSeparator(SwingConstants.HORIZONTAL), constraints);
		constraints.fill = GridBagConstraints.NONE;

		// total cash required
		totalCashRequiredLabel = new JLabel("Total Cash Required       ");
		totalCashRequiredLabel.setFont(appFont);
		constraints.gridx = 0;
		constraints.gridy = 4;
		constraints.gridwidth = 3;
		constraints.anchor = GridBagConstraints.WEST;
		this.add(totalCashRequiredLabel, constraints);
		totalCashRequiredField = new JTextField(4);
		totalCashRequiredField.addFocusListener(this);
		totalCashRequiredField.setFocusable(false);
		totalCashRequiredField.setFont(appFont);
		totalCashRequiredField.setBorder(BorderBox.fieldBorder);
		totalCashRequiredField
				.setToolTipText("<html>Total amount of cash required<br/>"
						+ "to close on the property.</html>");
		totalCashRequiredField.setHorizontalAlignment(JTextField.RIGHT);
		constraints.gridwidth = 1;
		constraints.gridx = 3;
		constraints.anchor = GridBagConstraints.EAST;
		this.add(totalCashRequiredField, constraints);

		updateFields();
	}

	public void updateFields() {
		this.downPaymentField.setText(dm.getDownPayment());
		this.purchaseCostsField.setText(dm.getPurchaseCosts());
		this.lessCashBackField.setText(dm.getLessCashBack());
		this.totalCashRequiredField.setText(dm.getTotalCashRequired());
	}

	public void updateModel() {
		dm.setLessCashBack(FieldParser.parseInteger(this.lessCashBackField
				.getText()));
		dm.updateInterface();
	}

	@Override
	public void focusGained(FocusEvent e) {
		if (e.getSource() instanceof JTextField) {
			JTextField focusedField = (JTextField) e.getSource();
			focusedField.selectAll();
		}

	}

	@Override
	public void focusLost(FocusEvent e) {
		updateModel();
	}

	@Override
	public void keyPressed(KeyEvent e) {
		if (e.getSource() == lessCashBackField) {
			if (e.getKeyCode() == KeyEvent.VK_DOWN) {
				fm.getAddressField().requestFocus();
			} else if (e.getKeyCode() == KeyEvent.VK_UP) {
				fm.getSellerRebateField().requestFocus();
			} else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
				fm.getCapRateField().requestFocus();
			} else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
				fm.getLicensesField().requestFocus();
			}
		}
		if (e.getKeyCode() == KeyEvent.VK_ENTER) {
			updateModel();
		}
	}

	@Override
	public void keyReleased(KeyEvent e) {
		

	}

	@Override
	public void keyTyped(KeyEvent e) {

	}

}
